﻿using DMSkin.WPF;
using log4net;
using MXComponent.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MXComponent.Views.system
{
    /// <summary>
    /// ChangePasswordPage.xaml 的交互逻辑
    /// </summary>
    public partial class ChangePasswordPage : Page
    {
        public static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public ChangePasswordPage()
        {
            InitializeComponent();
        }

        private void Btnok_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.txtOldPassword.Password))
            {
                MessageBox.Show("请输入原密码！");
                this.txtOldPassword.Focus();
                return;
            }
            if (string.IsNullOrWhiteSpace(this.txtNewPassword.Password))
            {
                MessageBox.Show("请输入新密码！");
                this.txtNewPassword.Focus();
                return;
            }
            if (string.IsNullOrWhiteSpace(this.txtRePassword.Password))
            {
                MessageBox.Show("请再次输入密码！");
                this.txtRePassword.Focus();
                return;
            }
            if (this.txtOldPassword.Password.Trim() != GlobalClass.user.password)
            {
                MessageBox.Show("原密码错误！");
                this.txtOldPassword.Focus();
                return;
            }
            if (this.txtNewPassword.Password != this.txtRePassword.Password)
            {
                MessageBox.Show("两次输入密码不一致！");
                this.txtNewPassword.Focus();
                return;
            }
            try
            {
                new userDB().ChangePassword(GlobalClass.user.userid, this.txtNewPassword.Password.Trim());
                MessageBox.Show("保存成功!");
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                MessageBox.Show(ex.Message);
            }
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            this.txtOldPassword.Focus();
        }
    }
}